## R Script Output -------------------------------------------------------------
# Appendix Figure A3: Changes in the Democratic Party Vote Shares in Presidential Elections.


## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to where README.txt is located
# Step 2: Run entire script


## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())
date()

# load packages
pkg <- c("tidyverse",  
         "RColorBrewer", 
         "gridExtra", 
         "viridis",
         "choroplethr", 
         "choroplethrMaps")
  
lapply(pkg, require, character.only = TRUE)

# set main directory
MAIN_DIR <- "~/Dropbox/Research/ISQ-frei-replication/"


## load data -------------------------------------------------------------------
load(file = paste(MAIN_DIR, "pres.RData", sep = "")) 
data(continental_us_states)


## Figure A3 -------------------------------------------------------------------
# set cut points
cut.points.change <- c(-1, -0.2, -0.1, -0.05, -0.01, 
                       0.01, 0.05, 0.1, 0.2)

cut.labels.change <- c("< -0.2", 
                       "[-0.2, -0.1)", 
                       "[-0.1, -0.05)",
                       "[-0.05, -0.01)", 
                       "[-0.01, 0.01)",
                       "[0.01, 0.05)",
                       "[0.05, 0.1)",
                       "[0.1, 0.2)")

# set color
brewer.pal(9, "Set1")
blue.white.pal <- colorRampPalette(c("#377EB8", "white"), alpha = TRUE)(5)
white.blue.pal <- colorRampPalette(c("white", "#377EB8"), alpha = TRUE)(5)[-1]
white.red.pal <- colorRampPalette(c("white", "#E41A1C"), alpha = TRUE)(5)[-1]
red.white.pal <- colorRampPalette(c("#E41A1C", "white"), alpha = TRUE)(5)
blue.white.red.pal <- c(red.white.pal, white.blue.pal)

# create vars for plotting
pres.p <- pres %>%
  complete(fips, year) %>%
  group_by(fips) %>%
  mutate(dem_share_08 = dem_share[year == 2008],
         dem_share_12 = dem_share[year == 2012],
         dem_share_16 = dem_share[year == 2016],
         dem_share_20 = dem_share[year == 2020]) %>%
  slice(1) %>%
  ungroup() 

# compute change 2012 to 2016
pres.12.16 <- pres.p %>%
  mutate(value_12_16 = dem_share_16 - dem_share_12) %>%
  rename(region = fips, 
         value = value_12_16) %>%
  mutate(value = cut(value, breaks = cut.points.change,
                     right = FALSE,
                     labels = cut.labels.change))

# plot 2012-2016
choro.d.vote.12.16 <- county_choropleth(pres.12.16,
                                        title = "2012-2016", 
                                        state_zoom = continental_us_states) +
  scale_fill_manual(values = blue.white.red.pal,
                    name = "Change in\nDem. Vote Share",
                    na.value = "grey90") +
  theme(plot.title = element_text(size = 20,
                                  face = "bold",
                                  hjust = 0.5))


# plot 2012-2020
pres.12.20 <- pres.p %>%
  mutate(value_12_20 = dem_share_20 - dem_share_12) %>%
  rename(region = fips, 
         value = value_12_20) %>%
  mutate(value = cut(value, breaks = cut.points.change,
                     right = FALSE,
                     labels = cut.labels.change))

# plot
choro.d.vote.12.20 <- county_choropleth(pres.12.20,
                                        title = "2012-2020", 
                                        state_zoom = continental_us_states) +
  scale_fill_manual(values = blue.white.red.pal,
                    name = "Change in\nDem. Vote Share",
                    na.value = "grey90") +
  theme(plot.title = element_text(size = 20,
                                  face = "bold",
                                  hjust = 0.5))


# plot 2008-2012
pres.08.12 <- pres.p %>%
  mutate(value_08_12 = dem_share_12 - dem_share_08) %>%
  rename(region = fips, 
         value = value_08_12) %>%
  mutate(value = cut(value, breaks = cut.points.change,
                     right = FALSE,
                     labels = cut.labels.change))

choro.d.vote.08.12 <- county_choropleth(pres.08.12,
                                        title = "2008-2012", 
                                        state_zoom = continental_us_states) +
  scale_fill_manual(values = blue.white.red.pal,
                    name = "Change in\nDem. Vote Share",
                    na.value = "grey90") +
  theme(plot.title = element_text(size = 20,
                                  face = "bold",
                                  hjust = 0.5))

# combine and save
pdf(file = paste(MAIN_DIR, "Figure-A3.pdf", sep = "/"), 
    width = 12, height = 14)
grid.arrange(choro.d.vote.08.12, choro.d.vote.12.16, choro.d.vote.12.20,  
             ncol = 1)
dev.off()


